
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Graphical.java
 *
 * Created on 2011-okt-09, 23:52:15
 */

/**
 *
 * @author Edvin
 */
public class Graphical extends javax.swing.JFrame {

	Board board;
	GameState gs;
	List<Move> lm;
	String solution;
	JPanel[][] e;
	GameState[] gsS;
    /** Creates new form Graphical */
    public Graphical(Board b, GameState gs, List<Move> lm, String solution) {
		this.board = b;
		this.gs = gs;
		this .lm = lm;
		this.solution = solution;
        initComponents();
		
		int xS = b.mCellRows;
		int yS = b.mCellCols;
		int cS = 600/yS;
		int nX = 600;
		int nY = xS*cS;
		setSize(nX, nY);
		

		jPanel1.setLayout(new GridLayout(b.mCellRows, b.mCellCols));
		e = new JPanel[b.mCellRows][b.mCellCols];

		//gen gsS
		gsS = new GameState[lm.size()+1];
		gsS[0] = new GameState(gs);
		for(int i = 0; i < lm.size(); i++){
			gsS[i+1] = new GameState(gsS[i]);
			gsS[i+1].doMove(board, lm.get(i));
		}

		for(int r = 0; r < b.mCellRows; r++){
			for(int c = 0; c < b.mCellCols; c++){
				e[r][c] = new JPanel();
				e[r][c].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
				setBoardSymbolAt(r, c);
				jPanel1.add(e[r][c]);
//				add(new Label("fisk"));
			}
		}

		jLabel2.setText(""+lm.size());
		jSlider1.setMaximum(lm.size());
//		jSlider1.setValue(solution.length());
		jSlider1.setValue(0);


		addComponentListener(new ComponentListener() {
			// This method is called after the component's size changes
			public void componentResized(ComponentEvent evt) {
				if(jCheckBox1.isSelected()){
					Component c = (Component)evt.getSource();
					// Get new size
					Dimension newSize = c.getSize();
					int xS = board.mCellRows;
					int yS = board.mCellCols;
					int cS = newSize.width/yS;
					int nX = newSize.width;
					int nY = xS*cS;
					setSize(nX, nY);
				}
			}

			public void componentMoved(ComponentEvent e) {
//				throw new UnsupportedOperationException("Not supported yet.");
			}

			public void componentShown(ComponentEvent e) {
//				throw new UnsupportedOperationException("Not supported yet.");
			}

			public void componentHidden(ComponentEvent e) {
//				throw new UnsupportedOperationException("Not supported yet.");
			}
		});


		setVisible(true);
    }

	void resetAllBoardSymbols(){
		for(int r = 0; r < board.mCellRows; r++){
			for(int c = 0; c < board.mCellCols; c++){
				setBoardSymbolAt(r, c);
			}
		}
	}

	void setBoardSymbolAt(int r, int c){
		Color col;
		if(board.isInvalid(r, c)){
			col = Color.DARK_GRAY;
		}else if(board.isWall(r, c)){
			col = Color.BLACK;
		}else if(board.isGoal(r, c)){
			col = Color.RED;
		}else{
			col = Color.WHITE;
		}
//				pr("col:"+col.toString());
		e[r][c].setBackground(col);
	}

	void setGSSymbolsAt(int move){
//		pr("value: "+move);
		resetAllBoardSymbols();
		for(int i = 0; i < gsS[move].pos.length; i++){
			byte b = gsS[move].gs[i];
			int[] pos = gsS[move].pos[i];
			if(GameState.isBlockAtGoal(board, gsS[move], pos)){
				e[pos[0]][pos[1]].setBackground(Color.GREEN);
			}else if(GameState.isBlock(b)){
				e[pos[0]][pos[1]].setBackground(Color.BLUE);
			}else if(GameState.isPlayerAtGoal(board, gsS[move], pos)){
				e[pos[0]][pos[1]].setBackground(Color.CYAN);
			}else if(GameState.isPlayer(b)){
				e[pos[0]][pos[1]].setBackground(Color.YELLOW);
			}
		}
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSlider1 = new javax.swing.JSlider();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 166, Short.MAX_VALUE)
        );

        jLabel2.setText("jLabel2");

        jLabel3.setText("0");

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Preserve dimensions");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jTextField1.setText("jTextField1");
        jTextField1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextField1CaretUpdate(evt);
            }
        });
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox1)
                .addGap(26, 26, 26)
                .addComponent(jLabel2))
            .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jCheckBox1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	boolean changeByUser = true;
	private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
		if(changeByUser){
			setGSSymbolsAt(jSlider1.getValue());
			jTextField1.setText(""+jSlider1.getValue());
		}
		changeByUser = true;
	}//GEN-LAST:event_jSlider1StateChanged

	private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
		if(jCheckBox1.isSelected()){
			Dimension newSize = getSize();
			int xS = board.mCellRows;
			int yS = board.mCellCols;
			int cS = newSize.width/yS;
			int nX = newSize.width;
			int nY = xS*cS;
			setSize(nX, nY);
		}
	}//GEN-LAST:event_jCheckBox1ActionPerformed

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
		if(isInt(jTextField1.getText())){
			changeByUser = false;
			jSlider1.setValue(Integer.parseInt(jTextField1.getText()));
			setGSSymbolsAt(jSlider1.getValue());
		}
	}//GEN-LAST:event_jTextField1ActionPerformed

	private void jTextField1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextField1CaretUpdate
		if(isInt(jTextField1.getText())){
			changeByUser = false;
			jSlider1.setValue(Integer.parseInt(jTextField1.getText()));
			setGSSymbolsAt(jSlider1.getValue());
		}
	}//GEN-LAST:event_jTextField1CaretUpdate

	boolean isInt(String s){
		try{
			Integer.parseInt(jTextField1.getText());
			return true;
		}catch(NumberFormatException e){

		}
		return false;
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
	static void pr(Object msg){
		System.out.println(""+msg);
	}
}
